package m202406.d08;

import java.util.HashSet;
import java.util.Set;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/8 21:34
 */
public class Q3 {

    public static void main(String[] args) {
        Q3 q3 = new Q3();
        q3.lengthOfLongestSubstring("aabaab!bb");
    }

    //1.这个题目是滑动窗口
    public int lengthOfLongestSubstring(String s) {
        if (s.length() <= 1) {
            return s.length();
        }
        int res = 1;
        Set<Character> set = new HashSet<>();
        set.add(s.charAt(0));
        for (int i = 1, start = 0; i < s.length(); i++) {
            res = Math.max(set.size(), res);
            char c = s.charAt(i);
            if (set.contains(c)) {
                for (int j = start; j < i; j++) {
                    set.remove(s.charAt(j));
                    start++;
                    if (!set.contains(c)) {
                        break;
                    }
                }
            }
            set.add(c);
        }
        return Math.max(res, set.size());
    }
}
